﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using UniRlv.COL;

namespace UniRlv.UI.CodeLook
{
    public static class AppHelper
    {

        #region 通用
        //压缩字节
        //1.创建压缩的数据流 
        //2.设定compressStream为存放被压缩的文件流,并设定为压缩模式
        //3.将需要压缩的字节写到被压缩的文件流
        public static byte[] CompressBytes(byte[] bytes)
        {
            using (MemoryStream compressStream = new MemoryStream())
            {
                using (var zipStream = new GZipStream(compressStream, CompressionMode.Compress))
                    zipStream.Write(bytes, 0, bytes.Length);
                return compressStream.ToArray();
            }
        }

        //解压缩字节
        //1.创建被压缩的数据流
        //2.创建zipStream对象，并传入解压的文件流
        //3.创建目标流
        //4.zipStream拷贝到目标流
        //5.返回目标流输出字节
        public static byte[] Decompress(byte[] bytes)
        {
            using (var compressStream = new MemoryStream(bytes))
            {
                using (var zipStream = new GZipStream(compressStream, CompressionMode.Decompress))
                {
                    using (var resultStream = new MemoryStream())
                    {
                        zipStream.CopyTo(resultStream);
                        return resultStream.ToArray();
                    }
                }
            }
        }

        /// <summary>
        /// 判断是否为整数
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsInteger(string s)
        {
            string pattern = @"^\d*$";
            return Regex.IsMatch(s, pattern);
        }

        /// <summary>
        /// 判断是否为整数
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsGoodRatio(string s)
        {
            string pattern = @"^[\d:]+$";
            return Regex.IsMatch(s, pattern);
        }
        #endregion

        #region 业务相关

        public static string[] GetGG(string gg)
        {
            return gg.Split(GetGGSplitter(), StringSplitOptions.RemoveEmptyEntries);
        }

        public static string[] GetGGSplitter()
        {
            return new string[] { ";", "；", ",", "，" };
        }

        public static string[] GetRatioSplitter()
        {
            return new string[] { ":", "："};
        }

        public static int GetTagLevel(string ratio)
        {
            return ratio.Split(GetRatioSplitter(), StringSplitOptions.None).Length;
        }

        /// <summary>
        /// 返回比例数字，比如1:20，则返回 1和20
        /// </summary>
        /// <param name="ratio"></param>
        /// <returns>数组长度等于包装级别</returns>
        public static int[] GetRatios(string ratio)
        {
            string[] ss = ratio.Split(GetRatioSplitter(), StringSplitOptions.None);
            int[] ri = new int[ss.Length];

            for (int i = 0; i < ss.Length; i++)
            {
                ri[i] = Convert.ToInt32(ss[i]);
            }

            return ri;
        }

        /// <summary>
        /// 四级关联的各级包装数量顺序ratio43,ratio32,ratio21,0
        /// 三级关联的各级包装数量顺序ratio32,ratio21,0
        /// 二级关联的各级包装数量顺序ratio21,0
        /// </summary>
        /// <param name="ratio"></param>
        /// <returns>数组长度等于包装级别</returns>
        public static int[] GetLevelRatios(string ratio)
        {
            int[] ri = GetRatios(ratio);

            for (int i = 0; i < ri.Length-1; i++)
            {
                ri[i] = ri[i+1]/ri[i];
            }

            if (ri.Length>1)
            {
                ri[ri.Length - 1] = 0;
            }

            return ri;
        }

        /// <summary>
        /// 获取各工位一级码比例:ratio41, ratio31, ration21
        /// </summary>
        /// <param name="station"></param>
        /// <param name="ratio"></param>
        /// <returns></returns>
        public static int GetStationRatio(int station, string ratio)
        {
            int[] ri = GetLevelRatios(ratio);

            switch (station)
            {
                case 1:
                    return 1;
                case 2:
                    return ri[ri.Length-2];
                case 3:
                    return ri[ri.Length - 3] * ri[ri.Length - 2];
                case 4:
                    return ri[0] * ri[1] * ri[2];
                default:
                    return 1;
            }
        }

        /// <summary>
        /// 获取各工位顶级码比例:ratio43, ratio42, ration41
        /// </summary>
        /// <param name="station"></param>
        /// <param name="ratio"></param>
        /// <returns></returns>
        public static int GetStationRatio2(int station, string ratio)
        {
            int[] ri = GetLevelRatios(ratio);
            int ret = 1;

            for (int i = station-1; i < ri.Length - 1; i++)
            {
                ret *= ri[i];
            }

            return ret;
        }

        /// <summary>
        /// 通过生产日期获取有效期
        /// </summary>
        /// <param name="scrq">生产日期</param>
        /// <param name="yxqdw">有效期单位</param>
        /// <param name="yxq">有效期</param>
        /// <returns></returns>
        public static DateTime SYZSGetYXQ(DateTime scrq, date_unit yxqdw, int yxq)
        {
            DateTime dtNow = scrq;
            switch (yxqdw)
            {
                case date_unit.DU_YEAR:
                    dtNow = dtNow.AddYears(yxq);
                    dtNow = dtNow.AddDays(-1);
                    break;
                case date_unit.DU_MONTH:
                    dtNow = dtNow.AddMonths(yxq);
                    dtNow = dtNow.AddDays(-1);
                    break;
                case date_unit.DU_DAY:
                    dtNow = dtNow.AddDays(yxq);
                    dtNow = dtNow.AddDays(-1);
                    break;
                case date_unit.DU_HOUR:
                    dtNow = dtNow.AddHours(yxq);
                    break;
                case date_unit.DU_MINUTE:
                    dtNow = dtNow.AddMinutes(yxq);
                    break;
                case date_unit.DU_SECOND:
                    dtNow = dtNow.AddSeconds(yxq);
                    break;
                default:
                    dtNow = dtNow.AddMonths(yxq);
                    break;
            }

            return dtNow;
        }

        #endregion


    }
}
